package com.common.core.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.Toast;

import com.common.core.R;


public class ToastUtils {

	private static Toast toast;
	private static LayoutInflater inflater;
	
	public static void showToast(int resId) {
		try {
			if (toast != null) {
				toast.cancel();
				toast = null;
			}
			if(inflater == null) {
				inflater = LayoutInflater.from(ContextProvider.getApplicationContext());
			}
			TextView tv = (TextView) inflater.inflate(R.layout.view_toast, null);
			tv.setText(resId);
			toast = new Toast(ContextProvider.getApplicationContext());
			toast.setView(tv);
			toast.setGravity(android.view.Gravity.CENTER, 0, 0);

			toast.setDuration(Toast.LENGTH_SHORT);
			toast.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void showToast(String msg) {
		try {
			if (toast != null) {
				toast.cancel();
				toast = null;
			}
			if(inflater == null) {
				inflater = LayoutInflater.from(ContextProvider.getApplicationContext());
			}
			TextView tv = (TextView) inflater.inflate(R.layout.view_toast, null);
			tv.setText(msg);
			toast = new Toast(ContextProvider.getApplicationContext());
			toast.setView(tv);
			toast.setGravity(android.view.Gravity.CENTER, 0, 0);

			toast.setDuration(Toast.LENGTH_SHORT);
			toast.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void cancelToast(){
		if (toast != null) {
			toast.cancel();
			toast = null;
		}
	}
	public static Toast makeToast(Context context, String string) {
		if(inflater == null) {
			inflater = LayoutInflater.from(context);
		}
		TextView tv = (TextView) inflater.inflate(R.layout.view_toast, null);
		tv.setText(string);
		Toast toast = new Toast(context);
		toast.setView(tv);
		toast.setGravity(android.view.Gravity.CENTER, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		return toast;
	}

	public static Toast makeToast(Context context, int resId) {
		return makeToast(context, context.getString(resId));
	}
		
	
}
